package com.sss.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.sss.controller.BaseController;
import com.sss.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class AccountController extends BaseController {
    @Resource(name = "adminService")
    AdminService adminService;

    @RequestMapping(value = "admin/account/logout",method = RequestMethod.GET)
    public String logout(HttpSession session){
        Object o =session.getAttribute("adminId");
        if (o==null){
            logger.info("无管理权限，返回管理员登录页");
        }else {
            session.removeAttribute("adminId");
            session.invalidate();
            logger.info("登录信息已清除，返回管理员登录页");
        }
        return "redirect:/admin/login";
    }

    //修改密码
    @ResponseBody
    @RequestMapping(value = "admin/chagepassword",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public String checkLogin(HttpSession session, @RequestParam String lastpassword, @RequestParam String newpassword){
        logger.info("检查管理员权限");
        Object adminId=checkAdmin(session);
        if (adminId==null){
            return "redirect:/admin/login";
        }

        Boolean IsChage=adminService.chagepassword(lastpassword,newpassword);

        JSONObject object=new JSONObject();
        if (IsChage){
            logger.info("密码修改成功");
            object.put("success",true);
        }else{
            logger.info("原密码错误");
            object.put("success",false);
        }
        return object.toJSONString();
    }
}
